package com.eig.courseweb.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eig.common.entity.course.EduSubject;
import com.eig.common.vo.Result;
import com.eig.courseweb.dao.SubjectDao;
import com.eig.courseweb.service.SubjectService;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：StudentLsj（李世界）
 * @date ：Created in 2023/01/12 下午 02:41
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class SubjectServiceImpl implements SubjectService {

    @Resource
    private SubjectDao subjectDao;

    @Override
    public Result findPidByid(String pid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",pid);
        EduSubject eduSubject = subjectDao.selectOne(wrapper);
        String parentId = eduSubject.getParentId();
        return new Result(2000,"查询成功",parentId);
    }
    List<String> list = new ArrayList<>();
    @Override
    public Result findsubjectByStatus() {
        LinkedList<EduSubject> children = null;
        List<EduSubject> fafter = null;
        List<EduSubject> content = subjectDao.getContent();
        if (ObjectUtil.isNotEmpty(content)) {
            //子菜单
            children = subjectDao.queryAll();
            fafter = content;
        } else {
            //子菜单
            children = subjectDao.queryAll();
            fafter =   subjectDao.getContent();
            for (int i = 0; i < fafter.size(); i++) {
                for (EduSubject subject : children) {
                    if (fafter.get(i).getId().equals(subject.getParentId())) {
                        fafter.get(i).setChildren(children);
                        list.add(fafter.get(i).getId());
                    }
                }
            }
            for (String s : list) {
                if (ObjectUtil.isNotEmpty(s)) {
                    fafter = fafter.stream().filter(index -> index.getId() == s).collect(Collectors.toList());
                }
            }
        }
        //一级菜单
        List<EduSubject> eduSubjects = fromPermission(fafter, children);
        return new Result(2000, "查询成功", eduSubjects);
    }
    private List<EduSubject> fromPermission(List<EduSubject> fullName, List<EduSubject> children) {
        List<EduSubject> arrayList = new ArrayList<>();
        //遍历所有menu对象，判断是否有孩子持续递归
        for (EduSubject permission : fullName) {
            //设置子目录
            EduSubject treeChildren = setTreeChildren(permission, children);
            arrayList.add(treeChildren);
        }
        return arrayList;
    }

    private EduSubject setTreeChildren(EduSubject permis, List<EduSubject> children) {
        //此集合封装所有孩子
        List<EduSubject> childrenAll = new ArrayList<>();
        for (EduSubject aclPermission : children) {
            if (aclPermission.getParentId().equals(permis.getId())) {
                //递归设置子菜单
                EduSubject treeChildren = setTreeChildren(aclPermission, children);
                childrenAll.add(treeChildren);
            }
        }
        permis.setChildren(childrenAll);
        return permis;
    }
}
